package com.baseframe.topic.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baseframe.auth.utils.AuthUtil;
import com.baseframe.mybatis.utils.PageInfo;
import com.baseframe.mybatis.utils.PageUtil;
import com.baseframe.tool.utils.data.FunUtil;
import com.baseframe.topic.entity.SnsTestTopic;
import com.baseframe.topic.entity.SnsTestTopicItem;
import com.baseframe.topic.entity.SnsTestTopicUser;
import com.baseframe.topic.entity.request.SnsTestTopicRequest;
import com.baseframe.topic.entity.vo.SnsTestTopicVO;
import com.baseframe.topic.service.SnsTestTopicItemService;
import com.baseframe.topic.service.SnsTestTopicService;
import com.baseframe.topic.mapper.SnsTestTopicMapper;
import com.baseframe.topic.service.SnsTestTopicUserResultService;
import com.baseframe.topic.service.SnsTestTopicUserService;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**

* @description 针对表【sns_test_topic】的数据库操作Service实现
* @createDate 2023-06-29 23:12:25
*/
@Service
@AllArgsConstructor
public class SnsTestTopicServiceImpl extends ServiceImpl<SnsTestTopicMapper, SnsTestTopic>
    implements SnsTestTopicService{

    private SnsTestTopicItemService snsTestTopicItemService;

    private SnsTestTopicUserService snsTestTopicUserService;

    private SnsTestTopicUserResultService snsTestTopicUserResultService;

    @Override
    public Page<SnsTestTopicVO> testTopicPage(PageInfo pageInfo, SnsTestTopicRequest request) {

        Page<SnsTestTopic> snsTestTopicPage = PageUtil.buildPage(pageInfo, SnsTestTopic.class);
        Page<SnsTestTopic> page = page(snsTestTopicPage, new LambdaQueryWrapper<SnsTestTopic>()
                .like(FunUtil.isNotEmpty(request.getName()), SnsTestTopic::getName, request.getName())
                .orderByDesc(SnsTestTopic::getCreateTime)
        );
        Page<SnsTestTopicVO> resultPage = new Page<>();
        BeanUtils.copyProperties(page,resultPage,"records");
        List<SnsTestTopicVO> records = new ArrayList<>();
        for (SnsTestTopic record : page.getRecords()) {
            SnsTestTopicVO snsTestTopicVO = new SnsTestTopicVO();
            BeanUtils.copyProperties(record,snsTestTopicVO);
            snsTestTopicVO.setTotalScore(snsTestTopicItemService.totalScore(record.getId()));
            records.add(snsTestTopicVO);
        }
        resultPage.setRecords(records);
        return resultPage;
    }

    @Override
    public Page<SnsTestTopicVO> getTestTopicPageList(PageInfo pageInfo, SnsTestTopicRequest request) {

        Page<SnsTestTopicVO> page = PageUtil.buildPage(pageInfo, SnsTestTopicVO.class);
        //插入用户编号，用于附带查询用户成绩
        request.setUserId(AuthUtil.getUserId());
        //获取分页数据
        Page<SnsTestTopicVO> resultPage =  this.getBaseMapper().getTestTopicPageList(page,request);

        for (SnsTestTopicVO record : resultPage.getRecords()) {
            record.setJoin(snsTestTopicUserService.hasJoin(record.getId()));
        }


        return resultPage;
    }

    @Override
    public SnsTestTopicVO getTestTopicDetails(SnsTestTopicRequest request) {

        return this.getBaseMapper().getTestTopicDetails(request);
    }
}




